package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.admin.dao.AdminLoginLogMapper;
import cn.ibobo.lease.admin.dao.AdminRoleMapper;
import cn.ibobo.lease.common.dto.AdminDTO;
import cn.ibobo.lease.admin.dto.AdminRoleDTO;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.admin.dao.AdminMapper;
import cn.ibobo.lease.admin.service.AdminService;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.Md5Util;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * 管理员表业务处理接口
 * @Author lililiang
 * @Date 2018-12-09 18:17:26
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminDTO, String> implements AdminService {

    private static final Log LOGGER = LogFactory.getLog(AdminServiceImpl.class);

    @Autowired
    private AdminMapper mapper;
    @Autowired
    private AdminRoleMapper roleMapper;
    @Autowired
    private AdminLoginLogMapper logMapper;

    @Override
    protected BaseMapper<AdminDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 管理员表列表
     * @Author lililiang
     * @Date 2018-12-09 18:17:26
     * @Param
     * @Return
     */
    @Override
    public List<AdminDTO> list4Page(AdminDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 管理员表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-09 18:17:26
     * @Param
     * @Return
     */
    @Override
    public List<AdminDTO> listByCondition(AdminDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询管理员表详情信息
     * @Author lililiang
     * @Date 2018-12-09 18:17:26
     * @Param
     * @Return
     */
    @Override
    public AdminDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存管理员表信息
     * @Author lililiang
     * @Date 2018-12-09 18:17:26
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(AdminDTO entity) {
        //1.对密码进行加密处理
        String passwd = entity.getPasswd();
        if(passwd != null && passwd.length() > 0){
            entity.setPasswd(Md5Util.md5Suffix(passwd));
        }
        //2.用户账号重复校验
        if(checkRepeat(entity)){
            return ResultEnum.ACCOUNT_REPEAT.getCode();
        }
        if(entity.getAid() == null){
            //新增管理员
            String aid = UuidUtil.getUuid();
            String rid = UuidUtil.getUuid();
            AdminRoleDTO roleDTO = new AdminRoleDTO();
            roleDTO.setAid(aid);
            roleDTO.setRid(rid);
            roleDTO.setId(UuidUtil.getUuid());
            if(roleMapper.add(roleDTO) == 0){
                return ResultEnum.FAIL.getCode();
            }
            entity.setAid(aid);
            if(mapper.add(entity) == 0){
                return ResultEnum.FAIL.getCode();
            }
        }else{
            AdminRoleDTO roleDTO = new AdminRoleDTO();
            roleDTO.setAid(entity.getAid());
            roleDTO.setRid(entity.getRid());
            if(roleMapper.editByAid(roleDTO) == 0){
                return ResultEnum.FAIL.getCode();
            }
            //修改管理员
            if(mapper.edit(entity) == 0){
                return ResultEnum.FAIL.getCode();
            }
        }
        return ResultEnum.SUCCESS.getCode();
    }

    /**
     * @Description 校验账号是否存在
     * @Author lililiang
     * @Date 2018/12/27 8:09
     * @Param [entity]
     * @Return boolean
     */
    private boolean checkRepeat(AdminDTO entity) {
        AdminDTO tempDTO = new AdminDTO();
        tempDTO.setStatus(null);
        tempDTO.setName(entity.getName());
        String aid = entity.getAid();
        tempDTO.setAid(aid);
        List<AdminDTO> adminDTOS = mapper.listByCondition(tempDTO);
        if(adminDTOS != null && (adminDTOS.size() > 1
                || (adminDTOS.size() ==1 && !adminDTOS.get(0).getAid().equals(aid)))){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除管理员表信息
     * @Author lililiang
     * @Date 2018-12-09 18:17:26
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

}
